//
//  HNCourseListCell.m
//  优学荟管理系统
//
//  Created by 星道 on 2017/10/7.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNCourseListCell.h"
#import <PNChart.h>
#import "HNStrengtheningClassApplicationCountView.h"

@implementation HNCourseListCell
{
    IBOutlet UILabel *_Title;
    IBOutlet UIButton *_ShangXiaJia;
    __weak IBOutlet NSLayoutConstraint *_ChartsView;
    __weak IBOutlet UIView *_ChartsContentView;
    __weak IBOutlet UIView *_OrgView; // 统计表View
    __weak IBOutlet NSLayoutConstraint *_OrgViewHeight; // 统计表View高
    PNBarChart *_barChart;
    __weak IBOutlet NSLayoutConstraint *_ContentViewHeight;
    __weak IBOutlet UIButton *_Open;
}

- (IBAction)QiDao:(UIButton *)sender {

    [self.Delegate Cell:self QianDaoWithModel:self.Model];
}

- (IBAction)Edit:(UIButton *)sender {

    [self.Delegate Cell:self EditWithModel:self.Model];
}

- (IBAction)Comment:(UIButton *)sender {

    [self.Delegate Cell:self CommentWithModel:self.Model];
}

/** 开设加强课 */
- (IBAction)ClickCreatCourse:(id)sender {
    
    self.ClickCreatCourse ? self.ClickCreatCourse([self.Model.ID stringValue]):nil;
}


- (IBAction)ShangJia:(UIButton *)sender {

    [self.Delegate Cell:self ShangXiaJiaWithModel:self.Model];
}

/** 查看老师 */                              
- (IBAction)SeeTeacher:(UIButton *)sender {

    [self.Delegate Cell:self SeeTeacherWithModel:self.Model];
}

- (void)setModel:(CourseListModel *)Model {

    _Model = Model;
    _Title.text = _Model.name;
    _ShangXiaJia.selected = [_Model.sellStatus integerValue] == 1 ? YES:NO;
//    _ChartsView.constant =  [AccountTool shareAccountTool].currentAccount.isReinforce ?  305:0;
    _OrgViewHeight.constant =  0;
    
    if ([AccountTool shareAccountTool].currentAccount.isReinforce) {
        // 为YES时显示图表
        [self.NumArr removeAllObjects];
        [self SetupCountViewOfModel:Model];
        
//        [self.TitleArr removeAllObjects];
//        [self.NumArr removeAllObjects];
//        [self.Model.ElementCountArr enumerateObjectsUsingBlock:^(HNCourseListChartsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            [self.TitleArr addObject:obj.content];
//            [self.NumArr addObject:obj.commentCount];
//        }];
//
//        for (NSInteger i=self.TitleArr.count; i<6; i++) {
//            [self.TitleArr addObject:@""];
//        }
//        for (NSInteger i=self.NumArr.count; i<6; i++) {
//            [self.NumArr addObject:@0];
//        }
//        [self BarChartTitleArr:self.TitleArr NumArr:self.NumArr];

        if ([_Model.Typexxxx isEqualToNumber:@10]) {
            
            _Open.hidden = YES;
        } else {
            
            if ([_Model.commonLessonId isEqualToNumber:@0]) {
                
                _Open.hidden = NO;
            } else {
                _Open.hidden = YES;
            }
        }
        
    }
    
    if ([AccountTool shareAccountTool].currentAccount.isReinforce) {
        if (![_Model.Typexxxx isEqualToNumber:@10]) {

            
            if ([_Model.commonLessonId isEqualToNumber:@0]) {
                
                _Open.hidden = NO;
            } else {
                _Open.hidden = YES;
            }

        }
    }
}

/** 设置统计表的View */
- (void)SetupCountViewOfModel:(CourseListModel *)Model {
    
    [Model.ElementCountArr enumerateObjectsUsingBlock:^(HNCourseListChartsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
   
        HNStrengtheningClassApplicationCountView *View = [[HNStrengtheningClassApplicationCountView alloc] initWithNum:obj.commentCount andTitle:obj.content andCountNum:Model.maxElementCount];
        [_OrgView addSubview:View];
        [View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_OrgView.mas_top).offset(44*idx);
            make.left.equalTo(_OrgView.mas_left).offset(-12);
            make.right.equalTo(_OrgView);
            make.height.equalTo(@44);
        }];
    }];
    _OrgViewHeight.constant += (Model.ElementCountArr.count * 44);
    _ContentViewHeight.constant += (_OrgViewHeight.constant);
    [self layoutIfNeeded];
}


//- (PNBarChart *)BarChartTitleArr:(NSArray *)TitleArr NumArr:(NSArray *)NumArr {
//    if (!_barChart) {
//        PNBarChart *barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _ChartsContentView.frame.size.height)];
//        barChart.chartMarginTop = 12;
//        barChart.chartMarginBottom = 24;
//
//        barChart.isShowNumbers = NO;
//        barChart.isGradientShow = YES;
//        _barChart = barChart;
//        [_ChartsContentView addSubview:barChart];
//    }
//    [_barChart setXLabels:TitleArr];
//    [_barChart setYValues:NumArr];
//    [_barChart setStrokeColors:self.ColorArr];
//    [_barChart strokeChart];
//
//    return _barChart;
//}


- (NSMutableArray *)TitleArr {
    if (!_TitleArr) {
        _TitleArr = [NSMutableArray array];
        _NumArr = [NSMutableArray array];
    }
    return _TitleArr;
}


- (NSMutableArray <UIColor *>*)ColorArr {
    if (!_ColorArr) {
        _ColorArr = [NSMutableArray array];
    }
    [_ColorArr removeAllObjects];
    
        __block CGFloat Index = [self.Model.maxElementCount integerValue];
        // 循环对象数组，给每个Model配颜色值，放进新数组
        [self.Model.ElementCountArr enumerateObjectsUsingBlock:^(HNCourseListChartsModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 用当前Model的
            CGFloat scale = [obj.commentCount floatValue]/Index;
            if (scale >= 0.75) {
                [_ColorArr addObject:ColorRGB(137, 231, 197)];
            } else if (scale < 0.78 && scale >= 0.5) {
                [_ColorArr addObject:ColorRGB(113, 209, 238)];
            } else if (scale < 0.5 && scale >= 0.25) {
                [_ColorArr addObject:ColorRGB(246, 201, 84)];
            } else {
                [_ColorArr addObject:ColorRGB(233, 87, 78)];
            }
        }];
    return _ColorArr;
}
@end
